jQuery(function(){	
	jQuery("textarea.elastic").elastic();
	
	var doingGoalCache = new Array();	
	var $doingPage = jQuery("div#doing div.page");
	var $doingPageLoader = jQuery("div#doing_paging_loader");
	var $doingNextButton = jQuery("a#next_doing");
	var $doingPrevButton = jQuery("a#prev_doing");	
	
	var doneGoalCache = new Array();	
	var $donePage = jQuery("div#done div.page");
	var $donePageLoader = jQuery("div#done_paging_loader");
	var $doneNextButton = jQuery("a#next_done");
	var $donePrevButton = jQuery("a#prev_done");
	
	bindDoingListPaging();
	bindDoneListPaging();
	
	function bindDoingListPaging(){
		var keyValues = {
		   "userId": $doingNextButton.attr("name"),
		   "status" : "USING",
		   "goalStatus": "DOING"
		};
		var orderProperty = "updateDate";
		bindGoNext($doingNextButton, $doingPrevButton, doingGoalCache, $doingPage, $doingPageLoader, 
					keyValues, orderProperty);
		bindGoPrev($doingPrevButton, doingGoalCache, $doingPage, $doingPageLoader);
	}
	
	function bindDoneListPaging(){
		var keyValues = {
		   "userId": $doneNextButton.attr("name"),
		   "status" : "USING",
		   "goalStatus": "FINISHED"
		};
		var orderProperty = "updateDate";
		bindGoNext($doneNextButton, $donePrevButton, doneGoalCache, $donePage, $donePageLoader, 
					keyValues, orderProperty);
		bindGoPrev($donePrevButton, doneGoalCache, $donePage, $donePageLoader);
	}
		
	function bindGoNext($next, $prev, cache, $page, $loader, keyValues, orderProperty){
		$next.click(function(){
			var $this = $next;
			
			$page.slideUp();
			$loader.fadeIn();
			
			var prevPage = $prev.attr("rel");
			prevPage = parseInt(prevPage);
			var nextPage = prevPage + 2;
			
			if(nextPage < cache.length){
				var pageContent = cache[nextPage];							
				$page.html(pageContent);							
				$prev.attr("rel", nextPage-1);
				
				$page.slideDown();
				$loader.fadeOut();
			}else{
				var lastOrderPropertyValue = $next.attr("rel");
				PagingManager.getPage(1, keyValues, orderProperty, lastOrderPropertyValue, function(result){
					if(result != null){
						//var $page = jQuery("div#doing div.page");
						var lastItemIndex = $page.find("div.listnumber:last").html();
						var pageItems = result.pageItems;
						var pageItem, html;
						var cacheLength = cache.length;				
						
						if(result.pageItems.length == 0){
							cache[cacheLength]=$page.html();
							$next.unbind('click').click(function(){
								var $this = jQuery(this);
								
								$page.slideUp();
								$loader.fadeIn();
								
								var gotoPage = $prev.attr("rel");						
								gotoPage = parseInt(gotoPage)+2;
								if(gotoPage < cache.length){
									var pageContent = cache[gotoPage];							
									$page.html(pageContent);							
									$prev.attr("rel", gotoPage-1);
								}
								
								$page.slideDown();
								$loader.fadeOut();
								
								return false;
							});
							
							var gotoPage1 = $prev.attr("rel");						
							gotoPage1 = parseInt(gotoPage1)+2;
							if(gotoPage1 < cache.length){
								var pageContent = cache[gotoPage1];							
								$page.html(pageContent);							
								$prev.attr("rel", gotoPage1-1);
							}
							
							$page.slideDown();
							$loader.fadeOut();
							
							return;
						}					
						
						lastItemIndex = parseInt(lastItemIndex)+1;
						cache[cacheLength]=$page.html();
						$prev.attr("rel",cacheLength);
						$page.html("");
						
						for(var i=0; i<pageItems.length; i++){
							pageItem = pageItems[i];
							html = buildGoalProgress(i+lastItemIndex, pageItem);
							$page.append(html);
						}
						$next.attr("rel", pageItem.updateDate);
						$loader.fadeOut();
						$page.slideDown();
					}
				});
			}
			
			return false;
		});
	}
	
	function bindGoPrev($prev, cache, $page, $loader){
		$prev.click(function(){
			var $this = $prev;
			var gotoPage = $this.attr("rel");
			gotoPage = parseInt(gotoPage);
			var pageContent = cache[gotoPage];
			
			if(gotoPage < 0)
				return false;
			
			if(cache.length < parseInt(gotoPage)+2){
				cache[gotoPage+1] = $page.html();
			}
					
			$page.slideUp();
			$loader.fadeIn();
			$page.html(pageContent);
			$loader.fadeOut();
			$page.slideDown();
			if(gotoPage >= 0){
				gotoPage = gotoPage -1;
			}
			$this.attr("rel", gotoPage)
			
			return false;
		});
	}
	function buildGoalProgress(index, pageItem){
		var html = jQuery("div#goal_list_placeholder").html();
		var percentPX = '<div class="center" style="width:'+pageItem.percent*183/100+'px"></div>';
		html = html.replace("@[index]@", index);
		html = html.replace(/\@\[goalId\]\@/g, pageItem.goalId);
		html = html.replace(/\@\[title\]\@/g, pageItem.title);
		html = html.replace(/\@\[entryCount\]\@/g, pageItem.entryCount);
		html = html.replace(/\@\[percent\]\@/g, pageItem.percent);
		html = html.replace(/\@\[percentPX\]\@/g, percentPX);
		html = html.replace(/\@\[userCount\]\@/g, pageItem.userCount);		
		
		html = parseOperator(html, pageItem);
		return html;
	}
	
	function parseOperator(html, pageItem){
		var flag = true;
		while(flag){
			var startIfPos = html.indexOf("@[if_");
			if(startIfPos < 0){
				return html;
			}
			var endStartIfPos = html.indexOf("]@");
			
			var operatorClause = html.substring(startIfPos+5, endStartIfPos);
			var items = operatorClause.split(";");
			if(items.length < 3)
				continue;
			
			var startEndIfPos = html.indexOf("@[/if_" + items[0]);
			if(startEndIfPos < 0){
				return html;
			}	
			
			var operator = "==";
			if(items[1] == "gt"){
				operator = ">";
			}else if(items[2] == "lt"){
				operator = "<";
			}
			var clause = "var rs = pageItem."+items[0]+operator+"'"+items[2]+"'"; 
			eval(clause);
			if(rs == false){
				html = html.substring(0, startIfPos) + html.substring(startEndIfPos+items[0].length+8);
			}else{
				html = html.substring(0, startIfPos) + html.substring(endStartIfPos+2, startEndIfPos) + html.substring(startEndIfPos+items[0].length+8); 
			}
		}
	}
	
	var waitingLoadDoing = false;
	var waitingLoadDone = false;
	
	jQuery("div.goalProcessTab a.leftTab").click(function(){
		var $this = jQuery(this);
		if(!$this.hasClass("selected")){
			$this.addClass("selected");
			jQuery("div.goalProcessTab a.rightTab").removeClass("selected");
			jQuery("div#goal_info div#done").hide();
			jQuery("div#loader").show();
			waitingLoadDoing = true;
			setTimeout(showDoingGoals, 300);
		}
		return false;
	});
	
	jQuery("div.goalProcessTab a.rightTab").click(function(){
		var $this = jQuery(this);
		if(!$this.hasClass("selected")){
			$this.addClass("selected");
			jQuery("div.goalProcessTab a.leftTab").removeClass("selected");
			jQuery("div#goal_info div#doing").hide();
			jQuery("div#loader").show();
			waitingLoadDone = true;
			setTimeout(showDoneGoals, 300);
		}
		return false;
	});
	
	function showDoingGoals(){
		if(waitingLoadDone == true){
			waitingLoadDoing = false;
			return;
		}
		jQuery("div#loader").hide();
		jQuery("div#goal_info div#doing").show();
		waitingLoadDoing = false;
	}
	
	function showDoneGoals(){
		if(waitingLoadDoing == true){
			waitingLoadDone = false;
			return;
		}
		jQuery("div#loader").hide();
		jQuery("div#goal_info div#done").show();
		waitingLoadDone = false;
	}

	function resetDoingGoalListNumber(){
		var index=2;
		jQuery("#doing .goal_list .listnumber").each(function(){
			jQuery(this).html(index);
			index++;
		});
	}
});

function thumb(statusId, up){
	UserManagerExt.thumbStatus(statusId, up, function(result){
		if(result == 1){
			var $thumbUp = jQuery(".thumbUp");
			var $countTU = $thumbUp.next("b");				
			var $thumbDown = jQuery(".thumbDown");
			var $countTD = $thumbDown.next("b");
			if(up==true && !$thumbUp.hasClass('thumbUpSelected')){
				$thumbUp.addClass("thumbUpSelected");
				$thumbDown.removeClass("thumbDownSelected");								
				$thumbDown.attr("href","javascript:thumb("+statusId+","+false+")");
			}else if(up==false && !$thumbDown.hasClass('thumbDownSelected')){
				$thumbUp.removeClass("thumbUpSelected");
				$thumbDown.addClass("thumbDownSelected");
				$thumbUp.attr("href","javascript:thumb("+statusId+","+true+")");
			}
			
			UserManagerExt.countThumbUp(statusId, function(result){
				$countTU.fadeIn().html(result);
			});
			
			UserManagerExt.countThumbDown(statusId, function(result){
				$countTD.fadeIn().html(result);
			});
		}
	});
}
